package com.lijie.bs_recruit.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lijie.bs_recruit.mapper.ApplicantsMapper;
import com.lijie.bs_recruit.mapper.PositionMapper;
import com.lijie.bs_recruit.model.Applicants;
import com.lijie.bs_recruit.model.Position;
import com.lijie.bs_recruit.service.ApplicantsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lijie
 * @since 2021-04-07
 */
@Service
public class ApplicantsServiceImpl extends ServiceImpl<ApplicantsMapper, Applicants> implements ApplicantsService {
    @Autowired
    PositionMapper positionMapper;

    @Override
    public Applicants seApplicantByIds(Integer userId, Integer positionId) {
        QueryWrapper<Applicants> wrapper = new QueryWrapper<>();
        wrapper.eq("userId",userId)
                .eq("positionId", positionId);
        Applicants applicants = baseMapper.selectOne(wrapper);
        return applicants;
    }

    //user查看所有的投递记录
    @Override
    public PageInfo<Applicants> selApplicantByuserId( Integer pageIndex,Integer userId) {
        QueryWrapper<Applicants> wrapper = new QueryWrapper<>();
        wrapper.eq("userId",userId);
        List<Applicants> applicants = baseMapper.selectList(wrapper);
        PageHelper.startPage(pageIndex,5);
        PageInfo<Applicants> pageInfo = new PageInfo<>(getCompanyUtil(applicants));
        return pageInfo;
    }
    //user查看通过初筛的投递记录 isEmpty=1 ,2  ,3  4
    @Override
    public PageInfo<Applicants> selApplicantByUserIndex( Integer pageIndex,Integer userId,Integer IsEmployIndex) {
        QueryWrapper<Applicants> wrapper = new QueryWrapper<>();
        wrapper.eq("userId",userId)
                .eq("isEmploy",IsEmployIndex);
        List<Applicants> applicants = baseMapper.selectList(wrapper);
        PageHelper.startPage(pageIndex,5);
        PageInfo<Applicants> pageInfo = new PageInfo<>(getCompanyUtil(applicants));
        return pageInfo;
    }

    @Override
    public PageInfo<Applicants> selApplicantByHrIndex(Integer pageIndex, Integer hrId,Integer IsEmployIndex) {
        List<Applicants> applicants = baseMapper.selApplicantsByHrId(hrId, IsEmployIndex);
        PageHelper.startPage(pageIndex,5);
        PageInfo<Applicants> pageInfo = new PageInfo<>(applicants);
        return pageInfo;
    }

    //hr设置简历的不同状态
    public boolean setApplicantIsEmploy(Applicants applicants){
        UpdateWrapper<Applicants> wrapper=new UpdateWrapper<>();
        wrapper.set("isEmploy", applicants.getIsEmploy())
                .eq("userId", applicants.getUserId())
                .eq("positionId", applicants.getPositionId());
        boolean update = this.update(wrapper);
        return update;
    }

    private List<Applicants> getCompanyUtil(List<Applicants> applicants){
        for(Applicants a:applicants){
            QueryWrapper<Position> q=new QueryWrapper<>();
            q.select("companyName","positionName","minSalary","maxSalary","city","positionId","companyId")
                    .eq("positionId", a.getPositionId());
            Position position = positionMapper.selectOne(q);
            a.setPosition(position);
        }
        return applicants;
    }
}
